package org.squirrel.modules.bigscreen.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.squirrel.common.api.vo.Result;
import org.squirrel.common.system.base.controller.SquirrelController;
import org.squirrel.modules.bigscreen.dto.ReportExcelDto;
import org.squirrel.modules.bigscreen.dto.ReportShareDto;
import org.squirrel.modules.bigscreen.entity.ReportExcel;
import org.squirrel.modules.bigscreen.service.ReportExcelService;
import org.squirrel.modules.bigscreen.service.ReportShareService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@Api(tags = "报表表格管理")
@RequestMapping("/reportExcel")
public class ReportExcelController extends SquirrelController<ReportExcel, ReportExcelService> {

    @Autowired
    private ReportExcelService reportExcelService;

    @Autowired
    private ReportShareService reportShareService;

    /**
     * 通过报表编码获得报表详情
     * 
     * @param reportCode
     * @return
     */
    @ApiOperation(value = "通过报表编码获得报表详情", notes = "通过报表编码获得报表详情")
    @GetMapping("/detailByReportCode/{reportCode}")
    public Result<?> detailByReportCode(@PathVariable String reportCode) {
    	try {
	        ReportExcelDto reportExcelDto = reportExcelService.detailByReportCode(reportCode);
	        return Result.OK("通过报表编码获得报表详情成功", reportExcelDto);
    	}catch(Exception e) {
    		log.error("通过报表编码获得报表详情异常", e);
			return Result.error("通过报表编码获得报表详情失败,原因:" + e.getMessage());
    	}
    }

    /**
     * 预览
     * 
     * @param reportExcelDto
     * @return
     */
    @ApiOperation(value = "预览", notes = "预览")
    @PostMapping("/preview")
    public  Result<?> preview(@RequestBody ReportExcelDto reportExcelDto) {
    	try {
	        ReportExcelDto result = reportExcelService.preview(reportExcelDto);
	        return Result.OK("预览成功", result);
    	}catch(Exception e) {
    		log.error("预览异常", e);
			return Result.error("预览失败,原因:" + e.getMessage());
    	}
    }

    /**
     * 导出excel
     * 
     * @param reportExcelDto
     * @return
     */
    @ApiOperation(value = "导出excel", notes = "导出excel")
    @PostMapping("/exportExcel")
    public Result<?> exportExcel(@RequestBody ReportExcelDto reportExcelDto) {
    	try {
    		Boolean exportRes = reportExcelService.exportExcel(reportExcelDto);
    		return Result.OK("导出成功，请稍后在文件管理中查看", exportRes);
    	}catch(Exception e) {
    		log.error("预览异常", e);
			return Result.error("预览失败,原因:" + e.getMessage());
    	}
    }

//    @PostMapping("/exportPdf")
//    public ResponseBean exportPdf(@RequestBody ReportExcelDto reportExcelDto) {
//        reportExcelService.exportPdf(reportExcelDto);
//        return ResponseBean.builder().code(ResponseCode.SUCCESS_CODE)
//                .build();
//    }

    /**
     * 分享报表
     * 
     * @param dto
     * @return
     */
    @ApiOperation(value = "分享报表", notes = "分享报表")
    @PostMapping("/share")
    public Result<?> share(@Validated @RequestBody ReportShareDto dto) {
    	try {
    		ReportShareDto reportShareDto = reportShareService.insertShare(dto);
    		return Result.OK("分享报表成功", reportShareDto);
    	}catch(Exception e) {
    		log.error("预览异常", e);
			return Result.error("预览失败,原因:" + e.getMessage());
    	}
    }
}
